package com.example.mapp.buildSql;

import java.util.Map;

public class ExportMappSql {

    /**
     * 根据电梯编码、与维保时间 查询维保记录
     * @param
     * @return
     */
    public String findTractionHalfMon(Map<String, Object> map){
        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT id as halfMonId,maintenance_end_date as maintenanceDate FROM ");
        if("1".equals(map.get("elevatorType")) || "5".equals(map.get("elevatorType"))){
            sb.append(" traction_half_mon where 1=1 ");
        }
        else if("4".equals(map.get("elevatorType")) || "6".equals(map.get("elevatorType"))){
            sb.append(" escalator_half_mon where 1=1 ");
        }
        else if("2".equals(map.get("elevatorType"))){
            sb.append(" hydpress_half_mon where 1=1 ");
        }
        else if("3".equals(map.get("elevatorType"))){
            sb.append(" debris_half_mon where 1=1 ");
        }

        String elevatorNum = map.get("elevatorNum") + "";
        if(elevatorNum != null && !"".equals(elevatorNum)){
            sb.append(" AND elevator_num = #{elevatorNum}");
        }

        String startDate = map.get("startDate") + "";
        if(startDate != null && !"".equals(startDate)){
            sb.append(" AND maintenance_end_date >= #{startDate}");
        }

        String endDate = map.get("endDate") + "";
        if(endDate != null && !"".equals(endDate)){
            sb.append(" AND maintenance_end_date <= #{endDate}");
        }
        return sb.toString();
    }


    /**
     * 根据电梯编码、救援时间查询救援记录
     * @param
     * @return
     */
    public String findEmergencyRescue(Map<String, Object> map){
        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT id ,rescue_end_time as rescueEndTime FROM ");
        sb.append(" EMERGENCY_RESCUE where 1=1 ");

        String elevatorNum = map.get("elevatorNum") + "";
        if(elevatorNum != null && !"".equals(elevatorNum)){
            sb.append(" AND elevator_num = #{elevatorNum}");
        }
        String startDate = map.get("startDate") + "";
        if(startDate != null && !"".equals(startDate)){
            sb.append(" AND rescue_end_time >= #{startDate}");
        }
        String endDate = map.get("endDate") + "";
        if(endDate != null && !"".equals(endDate)){
            sb.append(" AND rescue_end_time <= #{endDate}");
        }
        return sb.toString();
    }

    public String findSelfCheck(Map<String,Object> map){
        StringBuilder sb = new StringBuilder();
        sb.append("select A.report_url as reportUrl,A.check_time as checkTime,A.* from self_check A where 1=1 ");
        String elevatorNum = map.get("elevatorNum") + "";
        if(elevatorNum != null && !"".equals(elevatorNum)){
            sb.append(" AND A.elevator_num = #{elevatorNum}");
        }
        String startDate = map.get("startDate") + "";
        if(startDate != null && !"".equals(startDate)){
            sb.append(" AND A.check_time >= #{startDate}");
        }
        String endDate = map.get("endDate") + "";
        if(endDate != null && !"".equals(endDate)){
            sb.append(" AND A.check_time <= #{endDate}");
        }
        return sb.toString();
    }

    public String findRepairInfo(Map<String,Object> map){
        StringBuilder sb = new StringBuilder();
        sb.append("select  f.type_name as elevatorName,\n" +
                "        e.elevator_model as elevatorModel,\n" +
                "        e.build_addr as buildAddr,\n" +
                "        e.elevator_num as elevatorNum,\n" +
                "        p.party_name as maintenancePartyName,\n" +
                "        d.apply_type as applyType,\n" +
                "        d.recorder_name as recorderName,\n" +
                "        d.apply_time as applyTime,\n" +
                "        d.repair_time as repairTime,\n" +
                "        d.cost,\n" +
                "        a.resolve_date as resolveDate,\n" +
                "        a.repair_cost as repairCost,\n" +
                "        a.unrepaired_cause as unrepairedCause,\n" +
                "        a.fault_description as faultDescription,\n" +
                "        a.reason,\n" +
                "        a.replace_parts as replaceParts,\n" +
                "        a.remarks,\n" +
                "        a.is_repaired as isRepaired,\n" +
                "        s1.user_name as repairUserName,\n" +
                "        s2.user_name as approveUserName,\n" +
                "        s3.user_name as safeUserName,\n" +
                "        e.maintenance_party_id as maintenancePartyId,\n" +
                "        e.used_party_id as userdPartyId,\n" +
                "        e.maint_tel as maintTel\n" +
                "       from repair_info a\n" +
                "                      left join apply_repair d on a.apply_repair_id = d.id\n" +
                "                      left join elevator e on e.id = a.elevator_id\n" +
                "                      left join elevator_type f on f.id = e.elevator_type_id\n" +
                "                      left join party p on p.id = e.maintenance_party_id\n" +
                "                      left join sys_user s1 on a.repair_user_id=s1.id\n" +
                "                      left join sys_user s2 on d.approve_user_id=s2.id\n" +
                "                      left join sys_user s3 on e.safe_user_id = s3.id where 1=1");
        String elevatorNum = map.get("elevatorNum") + "";
        if(elevatorNum != null && !"".equals(elevatorNum)){
            sb.append(" AND e.elevator_num = #{elevatorNum}");
        }
        String startDate = map.get("startDate") + "";
        if(startDate != null && !"".equals(startDate)){
            sb.append(" AND a.a.resolve_date >= #{startDate}");
        }
        String endDate = map.get("endDate") + "";
        if(endDate != null && !"".equals(endDate)){
            sb.append(" AND a.resolve_date <= #{endDate}");
        }
        return sb.toString();
    }
}
